﻿/**
 * @Create time : 2014年9月12日19:08:12
 * @Author : magicrolan
 **/

using UnityEngine;
using CommandHandler;
using System;
using System.Timers;
using System.IO;
using Ini;

namespace OnlinePlayers
{
    //Make the classname unique so it doesn't interfere with other mods
    // Keep the MonoBehaviour though
    public class OnlinePlayers : MonoBehaviour
    {
        public int refreshDelayInSeconds = 60;
        public int configSize = 2;
        public static string messageOnResfresh = "-------------辉原网Unturned玩家在线状态-------------";
        public static string steamURL = "http://steamcommunity.com/profiles/";
        public static string getSteam64ID = "true";
        public static string playerExplode = "||||EXPLODE||||";
        public static string playerXYZ = "true";
        public string postUrl = "http://pvp.liasica.com/";
        public Timer resfreshTimer;

        public static void OutputTxtLine(string p) // 将字符串写入文件
        {
            StreamWriter writer = new StreamWriter("Unturned_Data/Managed/mods/OnlinePlayers/OnlinePlayers.txt", true);
            writer.WriteLine(p); // 每行一个
            writer.Close();
        }

        public static void OutputTxtInline(string p)
        {
            StreamWriter writer = new StreamWriter("Unturned_Data/Managed/mods/OnlinePlayers/OnlinePlayers.txt", true);
            writer.Write(p); // 同一行
            writer.Close();
        }

        private void GetOnlinePlayers(CommandArgs args) // 指令
        {
            OutputPlayers();
        }


        public static string GetTimeStamp() // 时间戳
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds).ToString();
        }



        public static void OutputPlayers() // 输出玩家
        {
            // string str = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            string str = GetTimeStamp(); // 以时间戳形式记录
            string strinfo = messageOnResfresh;
            StreamWriter writer = new StreamWriter("Unturned_Data/Managed/mods/OnlinePlayers/OnlinePlayers.txt", true);
            writer.WriteLine(strinfo);
            writer.WriteLine(str + " : " + "当前在线玩家");
            writer.Close();

            foreach (BetterNetworkUser user in UserList.users)
            {
                // NetworkChat.sendAlert(user.name); // 公告
                // Reference.Tell(args.sender.networkPlayer, user.name); // 私密
                OutputTxtInline(user.name);
                if (getSteam64ID == "true")
                {
                    OutputTxtInline(playerExplode + user.steamid + playerExplode + steamURL + user.steamid);
                }
                if (playerXYZ == "true")
                {
                    Vector3 playerPosition = user.position;
                    float x = playerPosition[0];
                    float y = playerPosition[1];
                    float z = playerPosition[2];
                    OutputTxtInline(playerExplode + x + "," + y + "," + z);
                }
                OutputTxtLine("");
            }
        }

        public void writeIniFile()
        {
            IniFile configFile = new IniFile("Unturned_Data/Managed/mods/OnlinePlayers/config.ini");
            configFile.IniWriteValue("Timer", "服务器玩家刷新时间", "60"); // 默认时间间隔60000毫秒 60秒 1分钟
            configFile.IniWriteValue("Config", "文件大小限制", "2"); // 文件大小限制 1024 * 1024字节 = 1Mb
            configFile.IniWriteValue("Config", "分割符", "-------------辉原网Unturned玩家在线状态-------------");
            configFile.IniWriteValue("Config", "记录64位ID", "true"); // true false
            configFile.IniWriteValue("Config", "记录坐标", "true"); // true false
            configFile.IniWriteValue("Config", "玩家信息分隔符", "||||EXPLODE||||");
        }

        public void onStart() // 开始函数
        {
            Directory.CreateDirectory("Unturned_Data/Managed/mods/OnlinePlayers"); // 生成配置目录
            if (!File.Exists("Unturned_Data/Managed/mods/OnlinePlayers/OnlinePlayers.txt")) // 生成记录
            {
                OutputTxtInline("");
            }

            if (!File.Exists("Unturned_Data/Managed/mods/OnlinePlayers/config.ini"))
            {
                writeIniFile();
            }


            IniFile configfile = new IniFile("Unturned_Data/Managed/mods/OnlinePlayers/config.ini");

            // 检查配置文档
            if (configfile.IniReadValue("Timer", "服务器玩家刷新时间").Length > 0)
            {
                this.refreshDelayInSeconds = Convert.ToInt32(configfile.IniReadValue("Timer", "服务器玩家刷新时间"));
            }
            if (configfile.IniReadValue("Config", "文件大小限制").Length > 0)
            {
                this.configSize = Convert.ToInt32(configfile.IniReadValue("Config", "文件大小限制"));
            }
            if (configfile.IniReadValue("Config", "分割符").Length > 0)
            {
                messageOnResfresh = configfile.IniReadValue("Config", "分割符");
            }
            if (configfile.IniReadValue("Config", "记录64位ID").Length > 0)
            {
                getSteam64ID = configfile.IniReadValue("Config", "记录64位ID");
            }
            if (configfile.IniReadValue("Config", "玩家信息分隔符").Length > 0)
            {
                playerExplode = configfile.IniReadValue("Config", "玩家信息分隔符");
            }
            if (configfile.IniReadValue("Config", "记录坐标").Length > 0)
            {
                playerXYZ = configfile.IniReadValue("Config", "记录坐标");
            }

            // 自动删除文件
            FileInfo Logfile = new FileInfo("Unturned_Data/Managed/mods/OnlinePlayers/OnlinePlayers.txt");
            // Logfile.Length.ToString();
            if (Logfile.Length >= (double)(this.configSize * 10000))
            {
                File.Delete("Unturned_Data/Managed/mods/OnlinePlayers/OnlinePlayers.txt");
                OutputTxtInline(""); // 重新生成
            }

            // 定时器
            this.resfreshTimer = new System.Timers.Timer((double)(this.refreshDelayInSeconds * 0x3e8));
            this.resfreshTimer.Elapsed += new ElapsedEventHandler(this.GetOnline);
            this.resfreshTimer.Enabled = true;

        }

        private void GetOnline(object sender, ElapsedEventArgs e)
        {
            OutputPlayers();
        }







        public void Start() //Will be called when this mod is loaded
        {
            onStart();
            CommandList.add(new Command(GetOnlinePlayers, "getplayers"));
        }

        public void Update()  //Will be called once per frame update
        {

        }

        public void OnGui()  //Will be called multiple times per frame update
        {

        }

    }
}
